﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace MediSIIM.BizComponent.ZheJiangSYKT
{
    public class YiBaoJKDLL
    {
        [DllImport("BargaingApplyV3_01050.dll", CharSet = CharSet.Ansi)]
        static extern int f_UserBargaingInitV3_01050(string lpData1,
                                                    [MarshalAs(UnmanagedType.VBByRefStr)]ref string lpMessage,
                                                    string lpData2);
        [DllImport("BargaingApplyV3_01050.dll", CharSet = CharSet.Ansi)]
        static extern int f_UserBargaingCloseV3_01050(string lpData1,
                                                     [MarshalAs(UnmanagedType.VBByRefStr)]ref string lpMessage,
                                                     string lpData2);
        [DllImport("BargaingApplyV3_01050.dll", CharSet = CharSet.Ansi)]
        static extern int f_UserBargaingApplyV3_01050(int nTradeTypeID,
                                                    double TradeID,
                                                    string lpData1,
                                                     [MarshalAs(UnmanagedType.VBByRefStr)]ref string lpMessage,
                                                     string lpData2);
        public static bool inted = false;
        /// <summary>
        /// 开始一个交易
        /// </summary>
        /// <param name="nTradeTypeID">交易类型</param>
        /// <param name="sInData">传入参数</param>
        /// <param name="ybyydm">医疗机构编号</param>
        public static int YbCall(int nTradeTypeID, string sInData, string ybyydm, ref string outdata)
        {
            if (!inted)
            {
                YbInit(ybyydm);
            }
            int result = f_UserBargaingApplyV3_01050(nTradeTypeID, 0, sInData, ref outdata, ybyydm);
            outdata = outdata.TrimEnd(' ', '\0').Trim('$');
            return result;
        }

        /// <summary>
        /// 初始化接口
        /// </summary>
        public static int YbInit(string ybyydm)
        {
            string ls_InData = string.Empty.PadRight(3000);
            int result = f_UserBargaingInitV3_01050("", ref ls_InData, ybyydm);
            if (result == 0 && ls_InData.Split('~')[0] == "0")
            {
                inted = true;
            }
            return 0;
        }

        public static int YbClose()
        {
            string ls_InData = string.Empty.PadRight(3000);
            return f_UserBargaingCloseV3_01050("", ref ls_InData, "");
        }
    }
}
